// pages/buys/comfirm.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    value: "",
    show: false,
    searchvalue: "",
    lists: [],
    active: 0
  },
  nouses() {
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/cart/checkout',
      header: {
        'X-Byesame-Token': wx.getStorageSync('token')
      },
      data: {
        cartId: wx.getStorageSync('cartId'),
        addressId: wx.getStorageSync('addressId'),
        couponId: -1,
        grouponRulesId: wx.getStorageSync('grouponRulesId'),
      },
      method: "GET",
      success: (res) => {

        if (res.data.errno === 0) {
          console.log(res.data.data);
          this.setData({
            address: res.data.data.checkedAddress,
            cartlist: res.data.data.checkedGoodsList,
            goodsTotalPrice: Number(res.data.data.goodsTotalPrice).toFixed(2),
            freightPrice: Number(res.data.data.freightPrice).toFixed(2),
            couponPrice: Number(res.data.data.couponPrice).toFixed(2),
            actualPrice: res.data.data.actualPrice * 100
          })
          wx.setStorageSync('addressId', res.data.data.addressId)
          wx.setStorageSync('couponId', res.data.data.couponId)
          wx.setStorageSync('cartId', res.data.data.cartId)
          wx.setStorageSync('grouponRulesId', res.data.data.grouponRulesId)

          console.log(this.data.cartlist)
          console.log(this.data.couponPrice);
          this.setData({
            show: false
          });
        } else {

        }
      },
      fail: (error) => {

      }
    });
  },
  onSearchChange(event) {
    this.setData({
      searchValue: event.detail
    });
  },
  goaddress() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  onSubmit() {

    // console.log(this.data.value);
    wx.setStorageSync('message', this.data.value)
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },
  onChanges(e) {
    // console.log(e);
    this.setData({
      value: e.detail
    })
  },
  onSearchClick() {
    if (!this.data.searchValue || this.data.searchValue.trim() === '') {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      });
      return;
    }

    this.doSearch(this.data.searchValue.trim());
  },

  onSearch(event) {
    const keyword = event.detail;
    if (!keyword.trim()) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      });
      return;
    }
    this.doSearch(keyword);
  },

  onRadioChange(event) {
    const selectedId = event.detail;
    const availableList = this.data.availableList.map(item => {
      return {
        ...item,
      };
    });

    this.setData({
      availableList
    });

  },
  doSearch() {
    wx.showToast({
      title: '兑换暂不支持',
      icon: 'none'
    });
  },
  showPopup() {
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/coupon/selectlist',
      header: {
        'X-Byesame-Token': wx.getStorageSync('token')
      },
      data: {
        cartId: 0,
        grouponRulesId: 0
      },
      method: "GET",
      success: (res) => {
        if (res.data.errno === 0) {
          this.setData({
            show: true
          });
          console.log(res.data.data);
          this.setData({

            availableList: res.data.data.list.filter(item => item.available),
            unavailableList: res.data.data.list.filter(item => !item.available)
          })
          console.log(this.data.availableList);

        } else {

        }
      },
      fail: (error) => {

      }
    });

  },
  onchoose(e) {
    const id = e.currentTarget.dataset.cid
    console.log(id);
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/cart/checkout',
      header: {
        'X-Byesame-Token': wx.getStorageSync('token')
      },
      data: {
        cartId: wx.getStorageSync('cartId'),
        addressId: wx.getStorageSync('addressId'),
        couponId: id,
        grouponRulesId: wx.getStorageSync('grouponRulesId'),
      },
      method: "GET",
      success: (res) => {

        if (res.data.errno === 0) {
          console.log(res.data.data);
          this.setData({
            address: res.data.data.checkedAddress,
            cartlist: res.data.data.checkedGoodsList,
            goodsTotalPrice: Number(res.data.data.goodsTotalPrice).toFixed(2),
            freightPrice: Number(res.data.data.freightPrice).toFixed(2),
            couponPrice: Number(res.data.data.couponPrice).toFixed(2),
            actualPrice: res.data.data.actualPrice * 100
          })
          wx.setStorageSync('addressId', res.data.data.addressId)
          wx.setStorageSync('couponId', res.data.data.couponId)
          wx.setStorageSync('cartId', res.data.data.cartId)
          wx.setStorageSync('grouponRulesId', res.data.data.grouponRulesId)

          console.log(this.data.cartlist)
          console.log(this.data.couponPrice);
          this.setData({
            show: false
          });
        } else {

        }
      },
      fail: (error) => {

      }
    });
  },
  onClose() {

    this.setData({
      show: false
    });
  },
  getCheck() {
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/cart/checkout',
      header: {
        'X-Byesame-Token': wx.getStorageSync('token')
      },
      data: {
        cartId: wx.getStorageSync('cartId') || 0,
        addressId: wx.getStorageSync('addressId') || 0,
        couponId: wx.getStorageSync('couponId') || 0,
        grouponRulesId: wx.getStorageSync('grouponRulesId') || 0,
      },
      method: "GET",
      success: (res) => {

        if (res.data.errno === 0) {
          console.log(res.data.data);
          this.setData({
            address: res.data.data.checkedAddress,
            cartlist: res.data.data.checkedGoodsList,
            goodsTotalPrice: Number(res.data.data.goodsTotalPrice).toFixed(2),
            freightPrice: Number(res.data.data.freightPrice).toFixed(2),
            couponPrice: Number(res.data.data.couponPrice).toFixed(2),
            actualPrice: res.data.data.actualPrice * 100
          })
          wx.setStorageSync('addressId', res.data.data.addressId)
          wx.setStorageSync('couponId', res.data.data.couponId)
          wx.setStorageSync('cartId', res.data.data.cartId)
          wx.setStorageSync('grouponRulesId', res.data.data.grouponRulesId)
          wx.setStorageSync('userCouponId', res.data.data.userCouponId)
          console.log(this.data.cartlist)
          console.log(this.data.couponPrice);
        } else {

        }
      },
      fail: (error) => {

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCheck()
    this.setData({
      active: 0
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getCheck()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})